package com.vnp.vchat;

import uk.co.senab.photoview.PhotoView;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.FailReason;
import com.nostra13.universalimageloader.core.assist.ImageLoadingListener;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.nostra13.universalimageloader.core.display.FadeInBitmapDisplayer;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.Toast;

public class ViewPhotoActivity extends Activity implements OnClickListener{
	
	
	DisplayImageOptions options = new DisplayImageOptions.Builder()
								.displayer(new FadeInBitmapDisplayer(500))
								.imageScaleType(ImageScaleType.IN_SAMPLE_INT)
								.resetViewBeforeLoading(true)
								.bitmapConfig(Bitmap.Config.RGB_565)
								.cacheOnDisc(false)
								.build();
	
	ImageButton btnClose;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
//		this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
		setContentView(R.layout.activity_view_photo);
		
		btnClose = (ImageButton) findViewById(R.id.btn_close);
		
		btnClose.setOnClickListener(this);
		
		PhotoView photo = (PhotoView) findViewById(R.id.img);
		
		String path = getIntent().getStringExtra("url");
		if(path.startsWith("http")) {
			ImageLoader.getInstance().displayImage(path, photo, options, new ImageLoadingListener() {
				
				@Override
				public void onLoadingStarted(String imageUri, View view) {
				}
				
				@Override
				public void onLoadingFailed(String imageUri, View view,
						FailReason failReason) {
					  String message = null;
                      switch (failReason.getType()) {
                              case IO_ERROR:
//                                      message = "Input/Output error";
                                    message = "Nguồn ảnh bị đang chờ xử lí. Vui lòng thử lại sau!";
                                      break;
                              case DECODING_ERROR:
                            	  	message = "Nguồn ảnh có thể bị lỗi. Vui lòng thử lại sau!";
//                                      message = "Image can't be decoded";
                                      break;
                              case NETWORK_DENIED:
//                                      message = "Downloads are denied";
                            	    message = "Nguồn ảnh có thể bị lỗi. Vui lòng thử lại sau!";
                                      break;
                              case OUT_OF_MEMORY:
//                            	  DiscCacheUtil.removeFromCache(imageUri, ImageLoader.getInstance().getDiscCache());
                            	  ImageLoader.getInstance().getMemoryCache().clear();
                            	  ImageLoader.getInstance().getDiscCache().clear();
//                                      message = "Out Of Memory error";
                                      message = "Nguồn ảnh có thể bị lỗi. Vui lòng thử lại sau!";
                                      break;
                              case UNKNOWN:
                            	  	  message = "Định dạng ảnh không được hỗ trợ.";
                                      message = "Unknown error";
                                      break;
                      }
                      Toast.makeText(ViewPhotoActivity.this, message, Toast.LENGTH_SHORT).show();
				}
				
				@Override
				public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
				}
				
				@Override
				public void onLoadingCancelled(String imageUri, View view) {
				}
			});
		} else{
			if (path.startsWith("file://")) {
				ImageLoader.getInstance().displayImage(path, photo,options);
			} else {
				Bitmap bm = BitmapFactory.decodeFile(path);
				photo.setImageBitmap(bm);
			}
			
			
		}
		
		/*photo.setOnTouchListener(new com.vnp.vchat.UIControls.OnSwipeTouchListener(){
			public void onSwipeRight() {
				Log.i("Thientv", "Vuot sang phai==========");
			}
			public void onSwipeLeft() {
				Log.i("Thientv", "Vuot sang trai==========");
			}
			public void onSwipeTop() {
				btnClose.setVisibility(View.GONE);
				finish();
				overridePendingTransition( R.anim.nochange, R.anim.push_up);
				Log.i("Thientv", "Vuot len tren==========");
				finish(); 
			}
			public void onSwipeBottom() {
				btnClose.setVisibility(View.GONE);
				finish();
				overridePendingTransition( R.anim.nochange, R.anim.push_down );
				Log.i("Thientv", "Vuot xuong duoi==========");
			}
		});*/
		
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_close:
			closeActivity();
			break;

		default:
			break;
		}
	}
	
	
	@Override
	public void onBackPressed() {
		closeActivity();
	}

	public void closeActivity(){
		ChatActivity.onShow = true;
		btnClose.setVisibility(View.GONE);
		finish();
		overridePendingTransition(R.anim.over_main_right, R.anim.trans_right_out);
	}
}
